package com.varian.boot.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

/**
 * @author ben
 * @since 2024/6/22
 */
public class WriteLongAsStringSerializer extends StdSerializer<Long> {

    protected WriteLongAsStringSerializer() {
        super(Long.class);
    }

    @Override
    public void serialize(Long value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
            gen.writeString(value.toString());
        } else {
            gen.writeNumber(value);
        }
    }
}
